define([
    'backbone',
    'template',
    'jquery',
    'text!addHtml'
], function(B,template,$,html) {
    var addView=B.View.extend({
        el:$('#container'),
        template:template.compile(html),
        initialize:function(){
            this.render();
        },
        render:function(){
            this.$el.html(this.template(this.model.toJSON()));
            var tempModel = this.model;
            $('input[type=text]').each(function(){
                $(this).val(tempModel.get($(this).attr('name')));
            })
            $('textarea').each(function(){
                $(this).val(tempModel.get($(this).attr('name')));
            })
        },
        events:{
            'submit':'submitHandle'
        },
        submitHandle:function(e){
            e.preventDefault();
            var arr = $('form').serializeArray();
            var data = _.reduce(arr,function(preVal,curVal){
                preVal[curVal.name] = curVal.value;
                return preVal;
            },{});
            this.model.set(data);
            this.model.save();
            this.listenToOnce(this.model,'sync',function(){
                window.location.href = 'index.html';
            })
        },
              
    })
    return addView;
});